﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;


namespace MyFloridaHouse.libSvOpenDistricts.MapShapes
{
    public delegate void RegionLabelDataChangedEventHandler(object sender, EventArgs e);
    public delegate void RegionLabelConditionsChangedDataEventHandler(object sender, EventArgs e);

    public class RegionLabelBase : INotifyPropertyChanged
    {
        #region events
        public event RegionLabelDataChangedEventHandler RegionLabelDataChanged;
        public event RegionLabelConditionsChangedDataEventHandler RegionLabelConditionsChanged;

        public void RaiseRegionLabelDataChanged()
        {
            if (RegionLabelDataChanged != null) { RegionLabelDataChanged(this, new EventArgs()); }
        }

        public void RaiseRegionLabelConditionsChanged()
        {
            if (RegionLabelConditionsChanged != null) { RegionLabelConditionsChanged(this, new EventArgs()); }
        }
        #endregion

        #region propertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region NotifyPropertyChanged Properties
        private bool _isRegionLabelOn = true;
        public bool isRegionLabelOn
        {
            get { return _isRegionLabelOn; }
            set
            {
                if (_isRegionLabelOn != value)
                {
                    _isRegionLabelOn = value;
                    NotifyPropertyChanged("isRegionLabelOn");
                    RaiseRegionLabelDataChanged();
                }
            }
        }
        #endregion

        
        internal RegionLabelAppearance appearance = new RegionLabelAppearance();

        public virtual UIElement BuildLabel(bool showName, string regionName, List<RegionDataDTO> regionDataList)
        {
            throw new NotImplementedException();
        }

        internal UIElement GetRegionLabelShape(string regionName, List<string> regionDataList)
        {
            Border labelBorder = new Border();
            try
            {
                labelBorder.Background = new SolidColorBrush(appearance.backgroundColor);
                labelBorder.CornerRadius = new CornerRadius(3);
                labelBorder.Opacity = .8;

                Grid labelGrid = new Grid();
                labelBorder.Child = labelGrid;
                int rowCount = 2;
                for (int i = 0; i < rowCount; i++)
                {
                    RowDefinition rd = new RowDefinition();
                    labelGrid.RowDefinitions.Add(rd);
                }
                int colCount = 1;
                for (int i = 0; i < colCount; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    labelGrid.ColumnDefinitions.Add(cd);
                }

                int row = 0;
                int column = 0;

                if (0 < regionName.Length)
                {
                    AddLabelName(labelGrid, row++, column, regionName);
                }

                if (isRegionLabelOn)
                {
                    AddLabelData(labelGrid, row, column, regionDataList);
                }
            }
            catch
            {
                // ignore error and return empty border
            }
            return labelBorder;
        }


        private void AddLabelName(Grid labelGrid, int row, int column, string regionName)
        {
            TextBlock nameTextBlock = MakeText(regionName, appearance.nameFont, FontWeights.ExtraBold, appearance.nameColor, appearance.nameFontSize);
            AddLabelCellValue(labelGrid, nameTextBlock, row, column);
        }

        private void AddLabelData(Grid labelGrid, int row, int column, List<string> dataList)
        {
            if (dataList != null)
            {
                foreach (var dat in dataList)
                {
                    TextBlock dataTextBlock = MakeText(dat, appearance.dataFont, FontWeights.ExtraBold, appearance.dataColor, appearance.dataFontSize);
                    AddLabelCellValue(labelGrid, dataTextBlock, row++, column);
                }
            }

        }

        private static void AddLabelCellValue(Grid labelGrid, TextBlock labelTextBlock, int row, int column)
        {
            labelTextBlock.SetValue(Grid.RowProperty, row);
            labelTextBlock.SetValue(Grid.ColumnProperty, column);
            labelGrid.Children.Add(labelTextBlock);
        }

        private TextBlock MakeText(string newText, string font, FontWeight fontWeight, Color fontColor, double fontSize)
        {
            TextBlock tb = new TextBlock();
            tb.Text = newText.Trim();
            tb.FontFamily = new FontFamily(font);
            tb.Margin = new Thickness(1);
            tb.FontSize = fontSize;
            tb.HorizontalAlignment = HorizontalAlignment.Center;
            tb.VerticalAlignment = VerticalAlignment.Center;
            tb.Foreground = new SolidColorBrush(fontColor);
            tb.FontWeight = new FontWeight();
            tb.FontWeight = fontWeight;
            tb.Padding = new Thickness(2);

            return tb;
        }

    }
}
